home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / refbib.el < prev    next >
Lisp/Scheme  |  1996-01-20  |  25KB  |  733 lines

  1. ;;; refbib.el --- convert refer-style references to ones usable by Latex bib
  2.  
  3. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Henry Kautz <kautz@research.att.com>
  6. ;; Keywords: bib, tex
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Use: from a buffer containing the refer-style bibliography,
  28. ;;   M-x r2b-convert-buffer
  29. ;; Program will prompt for an output buffer name, and will log
  30. ;; warnings during the conversion process in the buffer *Log*.
  31.  
  32. ;;; Change Log:
  33.  
  34. ;; HISTORY
  35. ;; 9/88, created H.Kautz
  36. ;; modified 1/19/89, allow books with editor but no author;
  37. ;;                   added %O ordering field;
  38. ;;                   appended illegal multiple fields, instead of 
  39. ;;                     discarding;
  40. ;;                   added rule, a tech report whose %R number
  41. ;;                     contains "ISBN" is really a book
  42. ;;                   added rule, anything with an editor is a book
  43. ;;                     or a proceedings
  44. ;;                   added 'manual type, for items with institution
  45. ;;                     but no author or editor
  46. ;;                   fixed bug so trailing blanks are trimmed
  47. ;;                   added 'proceedings type
  48. ;;                   used "organization" field for proceedings
  49. ;; modified 2/16/89, updated help messages
  50. ;; modified 2/23/89, include capitalize stop words in r2b stop words,
  51. ;;                   fixed problems with contractions (e.g. it's),
  52. ;;                   caught multiple stop words in a row
  53. ;; modified 3/1/89,  fixed capitalize-title for first words all caps
  54. ;; modified 3/15/89, allow use of " to delimit fields
  55. ;; modified 4/18/89, properly "quote" special characters on output
  56.  
  57. ;;; Code:
  58.  
  59. ;**********************************************************
  60. ; User Parameters
  61.  
  62. (defvar r2b-trace-on nil "*trace conversion")
  63.  
  64. (defvar r2b-journal-abbrevs
  65.    '(  
  66.        )
  67.    "  Abbreviation list for journal names.  
  68. If the car of an element matches a journal name exactly, it is replaced by
  69. the cadr when output.  Braces must be included if replacement is a
  70. {string}, but not if replacement is a bibtex abbreviation.  The cadr
  71. may be eliminated if is exactly the same as the car.  
  72.   Because titles are capitalized before matching, the abbreviation
  73. for the journal name should be listed as beginning with a capital 
  74. letter, even if it really doesn't.
  75.   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
  76. \(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
  77. \"Artificial Intelligence\", but would replace Ijcai81 with the 
  78. BibTeX macro \"ijcai7\".")
  79.  
  80. (defvar r2b-booktitle-abbrevs 
  81.    '(  
  82.        )
  83.    "  Abbreviation list for book and proceedings names.  If the car of
  84. an element matches a title or booktitle exactly, it is replaced by 
  85. the cadr when output.  Braces must be included if replacement is 
  86. a {string}, but not if replacement is a bibtex abbreviation.  The cadr 
  87. may be eliminated if is exactly the same as the car.  
  88.   Because titles are capitalized before matching, the abbreviated title
  89. should be listed as beginning with a capital letter, even if it doesn't.
  90.   For example, a value of '((\"Aij\" \"{Artificial Intelligence}\")
  91. \(\"Ijcai81\" \"ijcai7\")) would expand Aij to the text string
  92. \"Artificial Intelligence\", but would replace Ijcai81 with the 
  93. BibTeX macro \"ijcai7\".")
  94.  
  95. (defvar r2b-proceedings-list
  96.    '()
  97.    "  Assoc list of books or journals which are really conference proceedings,
  98. but whose name and whose abbrev expansion (as defined in `r2b-journal-abbrevs'
  99. and `r2b-booktitle-abbrevs') does not contain the words \"conference\" or
  100. \"proceedings\".  (Those cases are handled automatically.)
  101. The entry must match the given data exactly.
  102.   Because titles are capitalized before matching, the items in this list 
  103. should begin with a capital letter.
  104.   For example, suppose the title \"Ijcai81\" is used for the proceedings of
  105. a conference, and it's expansion is the BibTeX macro \"ijcai7\".  Then
  106. `r2b-proceedings-list' should be '((\"Ijcai81\") ...).  If instead its
  107. expansion were \"Proceedings of the Seventh International Conference
  108. on Artificial Intelligence\", then you would NOT need to include Ijcai81
  109. in `r2b-proceedings-list' (although it wouldn't cause an error).")
  110.  
  111. (defvar r2b-additional-stop-words
  112.      "Some\\|What"
  113.    "Words other than the `capitalize-title-stop-words'
  114. which are not to be used to build the citation key")
  115.  
  116.  
  117. (defvar r2b-delimit-with-quote
  118.   t
  119.   "*If true, then use \" to delimit fields, otherwise use braces")
  120.  
  121. ;**********************************************************
  122. ; Utility Functions
  123.  
  124. (defvar capitalize-title-stop-words
  125.    (concat
  126.       "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
  127.       "by\\|with\\|that\\|its")
  128.    "Words not to be capitalized in a title (unless they are the first
  129. word in the title)")
  130.  
  131. (defvar capitalize-title-stop-regexp
  132.    (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
  133.  
  134. (defun capitalize-title-region (begin end)
  135.    "Like `capitalize-region', but don't capitalize stop words, except the first."
  136.    (interactive "r")
  137.    (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
  138.       (unwind-protect
  139.      (save-restriction
  140.         (set-syntax-table text-mode-syntax-table)
  141.         (narrow-to-region begin end)
  142.         (goto-char (point-min))
  143.         (if (looking-at "[A-Z][a-z]*[A-Z]")
  144.            (forward-word 1)
  145.            (capitalize-word 1))
  146.         (while (re-search-forward "\\<" nil t)
  147.            (if (looking-at "[A-Z][a-z]*[A-Z]")
  148.           (forward-word 1)
  149.           (if (let ((case-fold-search t))
  150.              (looking-at capitalize-title-stop-regexp))
  151.              (downcase-word 1)
  152.              (capitalize-word 1)))
  153.            ))
  154.      (set-syntax-table orig-syntax-table))))
  155.  
  156.  
  157. (defun capitalize-title (s)
  158.    "Like capitalize, but don't capitalize stop words, except the first."
  159.    (save-excursion
  160.       (set-buffer (get-buffer-create "$$$Scratch$$$"))
  161.       (erase-buffer)
  162.       (insert s)
  163.       (capitalize-title-region (point-min) (point-max))
  164.       (buffer-string)))
  165.  
  166. ;*********************************************************
  167. (defun r2b-reset ()
  168.    "Unbind defvars, for debugging."
  169.    (interactive)
  170.    (makunbound 'r2b-journal-abbrevs)
  171.    (makunbound 'r2b-booktitle-abbrevs)
  172.    (makunbound 'r2b-proceedings-list)
  173.    (makunbound 'capitalize-title-stop-words)
  174.    (makunbound 'capitalize-title-stop-regexp)
  175.    (makunbound 'r2b-additional-stop-words)
  176.    (makunbound 'r2b-stop-regexp))
  177.  
  178. (defvar r2b-stop-regexp
  179.    (concat "\\`\\(\\(" 
  180.       r2b-additional-stop-words "\\|" capitalize-title-stop-words
  181.       "\\)\\('\\w*\\)?\\W+\\)*\\([A-Z0-9]+\\)"))
  182.  
  183.  
  184. (defun r2b-trace (&rest args)
  185.    (if r2b-trace-on
  186.       (progn
  187.      (apply (function message) args)
  188.      (sit-for 0))))
  189.  
  190. (defun r2b-match (exp)
  191.    "Returns string matched in current buffer."
  192.    (buffer-substring (match-beginning exp) (match-end exp)))
  193.  
  194. (defvar r2b-out-buf-name "*Out*" "*output from refer-to-bibtex" )
  195. (defvar r2b-log-name "*Log*" "*logs errors from refer-to-bibtex" )
  196. (defvar r2b-in-buf nil)
  197. (defvar r2b-out-buf nil)
  198. (defvar r2b-log nil)
  199.  
  200. (defvar r2b-error-found nil)
  201.  
  202. (setq r2b-variables '(
  203.             r2b-error-found
  204.               r2bv-author
  205.               r2bv-primary-author
  206.               r2bv-date
  207.               r2bv-year
  208.               r2bv-decade
  209.               r2bv-month
  210.               r2bv-title
  211.               r2bv-title-first-word
  212.               r2bv-editor
  213.               r2bv-annote
  214.               r2bv-t